package com.qy.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import com.qy.dao.SystemParamDao;
import java.util.List;
import java.util.Map;
import java.sql.Types;
import com.qy.db.DbParameter;
import java.util.HashMap;
import java.util.ArrayList;
import com.qy.db.pool.DBPoolMgr;
import com.qy.bean.Option;
import com.qy.bean.SystemParam;
import com.qy.db.BaseDao;

/**
* s_system_param daoImpl实现类的生成
* @author zhangwenlong
* @date Tue Jan 30 12:11:31 GMT+08:00 2018
*/ 
public class SystemParamDaoImpl extends BaseDao  implements SystemParamDao {

	@Override
	public boolean createSystemParam(SystemParam systemParam) {
		boolean result = false;
		if (systemParam.beginAdd()) {
			String sql = "insert into s_system_param (id,s_key,s_value,s_desc) values (?,?,?,?);";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.INTEGER, systemParam.getId()));
			param.put(2, new DbParameter(Types.VARCHAR, systemParam.getSKey()));
			param.put(3, new DbParameter(Types.VARCHAR, systemParam.getSValue()));
			param.put(4, new DbParameter(Types.VARCHAR, systemParam.getSDesc()));
			result = execNoneQuery(sql, param) > -1;
			systemParam.commitAdd(result);
		}
		return result;
	}

	@Override
	public List<SystemParam> getSystemParamList() {
		String sql = "select * from s_system_param;";
		PreparedStatement pstmt = execQuery(sql, null);
		ResultSet rs = null;
		List<SystemParam> infos = null;
		if (pstmt != null) {
			infos = new ArrayList<SystemParam>();
			try {
				rs = pstmt.executeQuery();
				while (rs.next()) {
					infos.add(resultTo(rs));
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return infos;
	}

	@Override
	public SystemParam getSystemParamById(int id) {
		String sql = "select * from s_system_param where id=?;";
		Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
		param.put(1, new DbParameter(Types.INTEGER, id));
		PreparedStatement pstmt = execQuery(sql, param);
		ResultSet rs = null;
		SystemParam systemParam = null;
		if (pstmt != null) {
			try {
				rs = pstmt.executeQuery();
				if(rs.next()){
					systemParam = resultTo(rs);
				}
			} catch (SQLException e) {
				System.out.println("执行出错：" + sql);
			} finally {
				closeConn(pstmt, rs);
			}
		}
		return systemParam;
	}

	@Override
	public boolean updateSystemParam(SystemParam systemParam) {
		boolean result = false;
		if (systemParam.beginUpdate()) {
			String sql = "update s_system_param set s_key=?,s_value=?,s_desc=? where id=?;";
			Map<Integer, DbParameter> param = new HashMap<Integer, DbParameter>();
			param.put(1, new DbParameter(Types.VARCHAR, systemParam.getSKey()));
			param.put(2, new DbParameter(Types.VARCHAR, systemParam.getSValue()));
			param.put(3, new DbParameter(Types.VARCHAR, systemParam.getSDesc()));
			param.put(4, new DbParameter(Types.INTEGER, systemParam.getId()));
			result = execNoneQuery(sql, param) > -1;
			systemParam.commitUpdate(result);
		}
		return result;
	}

	public SystemParam resultTo(ResultSet rs) throws SQLException {
		SystemParam systemParam = new SystemParam();
		systemParam.setId(rs.getInt("id"));
		systemParam.setSKey(rs.getString("s_key"));
		systemParam.setSValue(rs.getString("s_value"));
		systemParam.setSDesc(rs.getString("s_desc"));
		systemParam.setOp(Option.None);
		return systemParam;
	}

	@Override
	public Connection openConn(){
		return DBPoolMgr.getStrategyConn();
	}

}

